package com.ims.core.inconfig.service.impl;

import java.util.List;
import com.ims.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.inconfig.domain.TInWord;
import com.ims.core.inconfig.mapper.TInWordRuleMapper;
import com.ims.core.inconfig.domain.TInWordRule;
import com.ims.core.inconfig.service.ITInWordRuleService;

/**
 * 敏感词校验Service业务层处理
 *
 * @author IMS
 * @date 2024-05-24
 */
@Service
public class TInWordRuleServiceImpl implements ITInWordRuleService
{
    @Autowired
    private TInWordRuleMapper tInWordRuleMapper;

    /**
     * 查询敏感词校验
     *
     * @param id 敏感词校验主键
     * @return 敏感词校验
     */
    @Override
    public TInWordRule selectTInWordRuleById(Long id)
    {
        return tInWordRuleMapper.selectTInWordRuleById(id);
    }

    /**
     * 查询敏感词校验列表
     *
     * @param tInWordRule 敏感词校验
     * @return 敏感词校验
     */
    @Override
    public List<TInWordRule> selectTInWordRuleList(TInWordRule tInWordRule)
    {
        return tInWordRuleMapper.selectTInWordRuleList(tInWordRule);
    }

    /**
     * 新增敏感词校验
     *
     * @param tInWordRule 敏感词校验
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTInWordRule(TInWordRule tInWordRule)
    {
        tInWordRule.setCreateTime(DateUtils.getNowDate());
        int rows = tInWordRuleMapper.insertTInWordRule(tInWordRule);
        insertTInWord(tInWordRule);
        return rows;
    }

    /**
     * 修改敏感词校验
     *
     * @param tInWordRule 敏感词校验
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTInWordRule(TInWordRule tInWordRule)
    {
        tInWordRule.setUpdateTime(DateUtils.getNowDate());
        tInWordRuleMapper.deleteTInWordByRuleId(tInWordRule.getId());
        insertTInWord(tInWordRule);
        return tInWordRuleMapper.updateTInWordRule(tInWordRule);
    }

    /**
     * 批量删除敏感词校验
     *
     * @param ids 需要删除的敏感词校验主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInWordRuleByIds(Long[] ids)
    {
        tInWordRuleMapper.deleteTInWordByRuleIds(ids);
        return tInWordRuleMapper.deleteTInWordRuleByIds(ids);
    }

    /**
     * 删除敏感词校验信息
     *
     * @param id 敏感词校验主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInWordRuleById(Long id)
    {
        tInWordRuleMapper.deleteTInWordByRuleId(id);
        return tInWordRuleMapper.deleteTInWordRuleById(id);
    }

    /**
     * 新增敏感词信息
     *
     * @param tInWordRule 敏感词校验对象
     */
    public void insertTInWord(TInWordRule tInWordRule)
    {
        List<TInWord> tInWordList = tInWordRule.getTInWordList();
        String ruleId = tInWordRule.getRuleId();
        if (StringUtils.isNotNull(tInWordList))
        {
            List<TInWord> list = new ArrayList<TInWord>();
            for (TInWord tInWord : tInWordList)
            {
                tInWord.setRuleId(ruleId);
                list.add(tInWord);
            }
            if (list.size() > 0)
            {
                tInWordRuleMapper.batchTInWord(list);
            }
        }
    }
}
